import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class RankDBManager extends RankManager {

	private Map rankTypes;

	// -------------------------------------------------------------------
	public RankDBManager(Connection connection) throws DBManagerException {
		// construcor

		super(connection); // get connection by super class constructor
		loadRankTypes(); // load rank_types table to the MAP

	}

	// ----------------------------------------------------------------------
	public void loadRankTypes() throws DBManagerException {
		// load rank_types table to the MAP ( Hash Map )
		// where kye is rank column
		// value is Rank Object ( rank_name,credit,comission,min_start_deposit)

		Map mapRankTypes = new HashMap();

		ResultSet resultSet = null;

		Statement statement = null;
		boolean flag = false;
		Connection conn = getConnection(); // receive connection from
		// RankManager

		String query = ("SELECT * FROM rank_types");

		try {
			statement = conn.createStatement();

			resultSet = statement.executeQuery(query);

			while (resultSet.next()) {
				// Getting data from rows of Rank Types table
				long rangKey = resultSet.getLong("rank"); // key

				// get value and set it to MAP
				Rank rangValue = new Rank();

				String rankName = resultSet.getString("rank_name");
				rangValue.setRank_name(rankName);

				long credit = resultSet.getLong("credit");
				rangValue.setCredit(credit);

				float commission = resultSet.getFloat("commission");
				rangValue.setComission(commission);

				long minStartDeposit = resultSet.getLong("min_start_deposit");
				rangValue.setMin_start_deposit(minStartDeposit);

				// setting (key,value) data to the MAP
				mapRankTypes.put(rangKey, rangValue);
			}

			flag = true;

			System.out.println("[Rank types] was added sucssefuly to Map");

		} catch (SQLException e) {

			throw new DBManagerException("[Rank types ] wasn't set down in Map!");
		} finally {
			try {
				resultSet.close();
				statement.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}

		}

		rankTypes = mapRankTypes;

	}

	// ----------------------------------------------------------------------------
	public Account getComissionAndCreditForAccount(Account account) {
		// get updated Account object with Comission and Credit inform

		Long currCredit = null;
		double currComission = 0;
		Rank currRank;

		// sort key from this MAP
		Object[] key = rankTypes.keySet().toArray();
		Arrays.sort(key);

		// run over this MAP by sorted key
		for (int i = 0; i < key.length; i++) {

			currRank = (Rank) rankTypes.get(key[i]);

			// find credit and commission with appropriate money amount
			if (currRank.getMin_start_deposit() <= account.getAmount()) {
				currCredit = currRank.getCredit();
				currComission = currRank.getComission();
			}
		}

		// update credit and commission for account object
		account.setCredit(currCredit);
		account.setCommission(currComission);

		return account;
	}

	@Override
	public Long getRankByMoneyAmount(double moneyAmount) {
		// Get appropriate rank by money Amount

		Long rank = null;
		Rank currRank;

		// sort key from this MAP
		Object[] key = rankTypes.keySet().toArray();
		Arrays.sort(key);

		// run over this MAP by sorted key
		for (int i = 0; i < key.length; i++) {

			currRank = (Rank) rankTypes.get(key[i]);

			// find rank with appropriate money amount
			if (currRank.getMin_start_deposit() <= moneyAmount) {
				rank = (Long) key[i];
			}
		}

		return rank;

	}

}
